<script setup>
import {User, Lock} from '@element-plus/icons-vue'
import {reactive, ref} from "vue";
import router from "@/router/router.js";
import {login, logout} from "@/net/index.js";
import {useStore} from "@/store/store.js";


const store = useStore()
const loginFormRef = ref()
const loginForm = reactive({
  username: '',
  password: '',
  remember: false
})
const loginRules = {
  username: [
    {required: true, message: '请输入用户名', trigger: 'blur'},
    {min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur'}
  ],
  password: [
    {required: true, message: '请输入密码', trigger: 'blur'},
    {min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur'}
  ]
}

const userLogin = () => {
  loginFormRef.value.validate(valid => {
    if (valid) {
      login(loginForm.username, loginForm.password, loginForm.remember, () => {
        // 登录成功后跳转到首页
        router.push("/index")
      })
    }
  })
}

const userLogout = () => {
  logout(() => router.push("/"))
}
</script>

<template>
  <div style="text-align: center;margin: 0 20px;">
    <div style="margin-top: 70px">
      <div style="font-size: 25px;font-weight: bold;">登录</div>
      <div style="font-size: 13px;color: gray">您需要输入用户名和密码才能登录</div>
    </div>
    <div style="margin-top: 50px">
      <el-form :model="loginForm" ref="loginFormRef" :rules="loginRules">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" type="text" placeholder="用户名/邮箱">
            <template #prefix>
              <el-icon>
                <User/>
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="密码">
            <template #prefix>
              <el-icon>
                <Lock/>
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-row>
          <el-col :span="12" style="text-align: left">
            <el-form-item prop="remember">
              <el-checkbox v-model="loginForm.remember" label="记住密码"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" style="text-align: right">
            <el-link @click="router.push('/reset')">忘记密码？</el-link>
          </el-col>
        </el-row>
      </el-form>
      <div style="margin-top: 30px">
        <el-button type="primary" @click="userLogin()" style="width: 270px" plain>立即登录</el-button>
      </div>
      <el-divider>
        <span style="font-size: 13px; color: gray">还未注册</span>
      </el-divider>
      <div style="margin-top: 40px;margin-bottom: 90px">
        <el-button type="warning" @click="router.push('/register')" style="width: 270px" plain>立即注册</el-button>
      </div>

    </div>

  </div>

</template>

<style scoped>

</style>